//package com.xqq.service;
//
//import com.xqq.entity.TbTrackDO;
//import com.xqq.respository.TbTrackRepository;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import org.springframework.data.elasticsearch.core.SearchHits;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.stereotype.Service;
//
//import java.time.LocalDateTime;
//import java.time.ZoneId;
//import java.util.Date;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
////TODO ES
//@Service
//public class TrackService {
//
//
//    @Autowired
//    private TbTrackRepository repository;
//
//    @Autowired
//    private ElasticsearchRestTemplate elasticsearchRestTemplate;
//    // 保存轨迹记录
//    public TbTrackDO saveTrack(TbTrackDO track) {
//        return repository.save(track);
//    }
//
//    // 根据 ID 查找轨迹记录
//    public Optional<TbTrackDO> findById(Long id) {
//        return repository.findById(id);
//    }
//
//    // 根据时间范围查找轨迹记录
//    public List<TbTrackDO> findByDateRange(Date startDate, Date endDate) {
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                .must(QueryBuilders.rangeQuery("gmtUpdate").gte(startDate).lte(endDate));
//
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(queryBuilder)
//                .build();
//        // 使用 search 方法获取结果
//        SearchHits<TbTrackDO> searchHits = elasticsearchRestTemplate.search(searchQuery, TbTrackDO.class);
//        return searchHits.getSearchHits()
//                .stream()
//                .map(hit -> hit.getContent())
//                .collect(Collectors.toList());
//    }
//
//    // 使用 ElasticsearchRestTemplate 保存数据
//    public TbTrackDO saveTrackWithTemplate(TbTrackDO track) {
//        elasticsearchRestTemplate.save(track);
//        return track; // 返回保存的对象
//    }
//
//    public List<TbTrackDO> findByGmtUpdateRange(long startGmtUpdate, long endGmtUpdate) {
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                .must(QueryBuilders.rangeQuery("localDateTime").gte(startGmtUpdate).lte(endGmtUpdate));
//
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(queryBuilder)
//                .build();
//        // 使用 search 方法获取结果
//        SearchHits<TbTrackDO> searchHits = elasticsearchRestTemplate.search(searchQuery, TbTrackDO.class);
//        return searchHits.getSearchHits()
//                .stream()
//                .map(hit -> hit.getContent())
//                .collect(Collectors.toList());
//    }
//    public List<TbTrackDO> findByGmtUpdateRange(LocalDateTime startGmtUpdate, LocalDateTime endGmtUpdate) {
//
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                .must(QueryBuilders.rangeQuery("localDateTime").gte(localDateTimeToTimestamp(startGmtUpdate)).lte(localDateTimeToTimestamp(endGmtUpdate)));
//
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(queryBuilder)
//                .build();
//        // 使用 search 方法获取结果
//        SearchHits<TbTrackDO> searchHits = elasticsearchRestTemplate.search(searchQuery, TbTrackDO.class);
//        return searchHits.getSearchHits()
//                .stream()
//                .map(hit -> hit.getContent())
//                .collect(Collectors.toList());
//    }
//    public List<TbTrackDO> findByGmtUpdateRange(String startGmtUpdate, String endGmtUpdate) {
//
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                .must(QueryBuilders.rangeQuery("localDateTime").gte(startGmtUpdate).lte(endGmtUpdate));
//
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(queryBuilder)
//                .build();
//        // 使用 search 方法获取结果
//        SearchHits<TbTrackDO> searchHits = elasticsearchRestTemplate.search(searchQuery, TbTrackDO.class);
//        return searchHits.getSearchHits()
//                .stream()
//                .map(hit -> hit.getContent())
//                .collect(Collectors.toList());
//    }
//    public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
//        return localDateTime.atZone(ZoneId.systemDefault())
//                .toInstant()
//                .toEpochMilli();
//    }
//}